package com.lam.datasave;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;


public class SharePreferencesActivity extends Activity{

	public final static String COLUM_NAME = "name" ;
	public final static String COLUM_MOBIE = "mobile";
	SharedPreferencesHelper sph;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	//	setContentView(R.layout.ds_sharedprefereces);
		sph = new SharedPreferencesHelper(this, "contacts");
		sph.putValue(COLUM_NAME, "Mr Wang");
		sph.putValue(COLUM_MOBIE, "123456789");
		
		String name=sph.getValue(COLUM_NAME);
		String moblie=sph.getValue(COLUM_MOBIE);
		
		LinearLayout layout=new LinearLayout(this);
		
		setContentView(layout);
		TextView tv= new TextView(this);
		tv.setText("Name:"+name+"\n"+"Moble:"+moblie);
		layout.addView(tv);
	}
	
	class SharedPreferencesHelper{
		SharedPreferences spPreferences;
		Editor editor;
		Context context;
		public SharedPreferencesHelper(Context c,String name){
			context = c;
			spPreferences = context.getSharedPreferences(name, 0);
			editor = spPreferences.edit();
		}
		
		public void putValue(String key,String value){
			editor = spPreferences.edit();
			editor.putString(key, value);
			editor.commit();
		}
		
		public String getValue(String key){
			return spPreferences.getString(key, null);
		}
	}
}
